﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static LiWi.Shell;
namespace LiWi
{
    public partial class 上位机和Basic交互 : Form
    {
        public WindowsAdapt adapt = new WindowsAdapt();
        IntPtr _handle { get { return Data.handle; } }
        bool isConnect { get { return Data.handle == (IntPtr)0 ? false : true; } }
        bool isDownBasic = false;
        public 上位机和Basic交互()
        {
            InitializeComponent();
            comboBox_function.SelectedIndex = 0;
            adapt.SetForm(this);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (isConnect)
            {
                if (!isDownBasic) {
                    int ret=  PMC_BasDown(_handle, "上位机与Basic交互.bas", 1);
                    isDownBasic = true;
                }
                float pos = 0;
                for (int i = 0; i < 3; i++)
                {
                    PMC_GetDpos(_handle, i, ref pos);
                    if (i == 0)
                    {
                        label_x.Text = string.Format("AXIS0:{0}", pos);
                    }
                    else if (i == 1)
                    {
                        label_y.Text = string.Format("AXIS1:{0}", pos);
                    }
                    else if (i == 2)
                    {
                        label_z.Text = string.Format("AXIS2:{0}", pos);
                    }

                    float[] values = new float[6];
                    float cmddata = 0;
                    PMC_GetUserArray(_handle, "array_para", 0, 6, values);
                    PMC_GetUserVar(_handle, "cmd_data", ref cmddata);
                    array_get1.Text = Convert.ToString(values[0]);
                    array_get2.Text = Convert.ToString(values[1]);
                    array_get3.Text = Convert.ToString(values[2]);
                    array_get4.Text = Convert.ToString(values[3]);
                    array_get5.Text = Convert.ToString(values[4]);
                    array_get6.Text = Convert.ToString(values[5]);
                    m_cmd.Text = Convert.ToString(cmddata);
                }

                button1.Enabled = true;
                button2.Enabled = true;
                button3.Enabled = true;
                button_crear.Enabled = true;
                button_stop.Enabled = true;
            }
            else {
                button1.Enabled = false;
                button2.Enabled = false;
                button3.Enabled = false;
                button_crear.Enabled = false;
                button_stop.Enabled = false;
            }
        }

        private void array_set1_TextChanged(object sender, EventArgs e)
        {

            TextBox t = (TextBox)sender;
            double value = 0;
            if (t != null) {
                if (!double.TryParse(t.Text, out value)) {
                    return;
                }
            }
            if (sender == array_set1)
            {
                PMC_SetUserVar(_handle, "array_para(0)", (float)value);
            }
            else if (sender == array_set2) {
                PMC_SetUserVar(_handle, "array_para(1)", (float)value);
            }
            else if (sender == array_set3)
            {
                PMC_SetUserVar(_handle, "array_para(2)", (float)value);
            }
            else if (sender == array_set4)
            {
                PMC_SetUserVar(_handle, "array_para(3)", (float)value);
            }
            else if (sender == array_set5)
            {
                PMC_SetUserVar(_handle, "array_para(4)", (float)value);
            }
            else if (sender == array_set6)
            {
                PMC_SetUserVar(_handle, "array_para(5)", (float)value);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PMC_Rapidstop(_handle, 3);
            for (int i = 0; i < 3; ++i)
            {
                int enable = 0;
                PMC_GetAxisEnable(_handle, i, ref enable);
                if (enable == 0)
                {
                    MessageBox.Show("请回连接处将需要运动的轴使能！！！");
                    return;
                }
            }
            if (isConnect) {
                PMC_SetUserVar(_handle, "cmd_data", 1);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            PMC_Rapidstop(_handle, 3);
            for (int i = 0; i < 3; ++i)
            {
                int enable = 0;
                PMC_GetAxisEnable(_handle, i, ref enable);
                if (enable == 0)
                {
                    MessageBox.Show("请回连接处将需要运动的轴使能！！！");
                    return;
                }
            }
            if (isConnect)
            {
                PMC_SetUserVar(_handle, "cmd_data", 2);
            }
        }

        private void button_stop_Click(object sender, EventArgs e)
        {

            if (isConnect)
            {
                PMC_SetUserVar(_handle, "cmd_data", 3);
            }
        }

        private void button_crear_Click(object sender, EventArgs e)
        {
            if (isConnect)
            {
                PMC_SetUserVar(_handle, "cmd_data", 4);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (isConnect) {

                if (GetProcIsFree(2))
                {
                    byte[] res = new byte[2048];
                    PMC_Execute(_handle, string.Format("RUNTASK 2,{0}", comboBox_function.Text), res, 2048);
                }
                else {
                    MessageBox.Show("任务2在运行中,请等待停止后启动");
                
                }
            }
        }

        private bool GetProcIsFree(uint taskNumber) 
        {
            byte[] res = new byte[2048];
            PMC_Execute(_handle, string.Format("?PROC_STATUS({0})", taskNumber), res, 2048);
            string str = System.Text.ASCIIEncoding.Default.GetString(res);
            int state = Convert.ToInt32(str.Trim());
            return state==0? true: false;
        }
    }
}
